{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('tom', '50', 'biker')\n",
      "\n",
      "By index:  jerry\n",
      "\n",
      "tom is 50 years of age.\n",
      "jerry is 25 years of age.\n"
     ]
    }
   ],
   "source": [
    "# Normal way of defining and using tuples\n",
    "\n",
    "first = ('tom', '50', 'biker')\n",
    "second = ('jerry', '25', 'doctor')\n",
    "\n",
    "print(first)\n",
    "print()\n",
    "print('By index: ', second[0])\n",
    "print()\n",
    "for i in [first, second]:\n",
    "    print('{} is {} years of age.'.format(*i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Person(name='tom', age='50', occ='biker')\n",
      "\n",
      "By index:  jerry\n",
      "\n",
      "tom is 50 years of age\n",
      "jerry is 25 years of age\n"
     ]
    }
   ],
   "source": [
    "import collections\n",
    "\n",
    "Person = collections.namedtuple('Person', 'name age occ')\n",
    "\n",
    "tom = Person(name='tom', age='50', occ='biker')\n",
    "jerry = Person(name='jerry', age='25', occ='doctor')\n",
    "\n",
    "print(tom)\n",
    "print()\n",
    "print('By index: ', jerry.name)\n",
    "print()\n",
    "for i in [tom, jerry]:\n",
    "    print('{} is {} years of age'.format(*i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type names and field names cannot be a keyword: 'class'\n",
      "\n",
      "Encountered duplicate field name: 'age'\n"
     ]
    }
   ],
   "source": [
    "# Invlaid field names\n",
    "\n",
    "# python keywords as attributes\n",
    "try:\n",
    "    collections.namedtuple('Person', 'age class occ')\n",
    "except ValueError as err:\n",
    "    print(err)\n",
    "\n",
    "    \n",
    "print()\n",
    "# duplicate attributes not allowed\n",
    "try:\n",
    "    collections.namedtuple('Person', 'age age occ')\n",
    "except ValueError as err:\n",
    "    print(err)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Representation:  Person(name='tom', age='50', occ='biker')\n",
      "Fields: ('name', 'age', 'occ')\n"
     ]
    }
   ],
   "source": [
    "# Special attributes - _fields, _asdict(), _replace()\n",
    "\n",
    "print('Representation: ', tom)\n",
    "print('Fields:', tom._fields)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OrderedDict([('name', 'tom'), ('age', '50'), ('occ', 'biker')])\n",
      "\n",
      "OrderedDict([('name', 'jerry'), ('age', '25'), ('occ', 'doctor')])\n"
     ]
    }
   ],
   "source": [
    "# Converting namedtuple to ordereddicts using _asdict() method\n",
    "\n",
    "print(tom._asdict())\n",
    "print()\n",
    "print(jerry._asdict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Before:  Person(name='tom', age='50', occ='biker')\n",
      "After replace:  Person(name='Steven', age='50', occ='biker')\n"
     ]
    }
   ],
   "source": [
    "# Replacing fields using _replace method\n",
    "# Returns another object rather than making changes in the same tuple object\n",
    "\n",
    "print('Before: ', tom)\n",
    "tom2 = tom._replace(name='Steven')\n",
    "print('After replace: ', tom2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "# Check for same object\n",
    "print(tom is tom2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
